package com.yanqu.road.dao.impl.cross.banquet;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.banquet.baifubanquet.GoodBanquetJoinInfoData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author cwq
 * @Data 2021/11/15 15:15
 */
public class CrossGoodBanquetJoinDaoImpl extends TempDataObjectDao<GoodBanquetJoinInfoData> {

    public static String getGoodBanquetJoinInfoSql = "select * from `t_c_activity_good_banquet_join` where `activityId`=?   order by  `time` asc";
    public List<GoodBanquetJoinInfoData> getGoodBanquetJoinInfoList(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<GoodBanquetJoinInfoData> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getGoodBanquetJoinInfoSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                list.add(parse(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String updateGoodBanquetJoinInfoSql = "update `t_c_activity_good_banquet_join` set userId=?,time=? where banquetId=? and activityId=?";
    @Override
    protected boolean updateData(GoodBanquetJoinInfoData goodBanquetJoinInfo) {
        goodBanquetJoinInfo.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, goodBanquetJoinInfo.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, goodBanquetJoinInfo.getTime()));
        para.put(i++, new DbParameter(Types.BIGINT, goodBanquetJoinInfo.getBanquetId()));
        para.put(i++, new DbParameter(Types.INTEGER, goodBanquetJoinInfo.getActivityId()));
        if (getDb().executeNoneQuery(updateGoodBanquetJoinInfoSql, para) > 0) {
            return true;
        }
        return false;
    }

    public boolean batchUpdate(List<GoodBanquetJoinInfoData> list) {
        getDb().setConnName(DbNameStrategy.getCross2Db());
        List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
        for (GoodBanquetJoinInfoData goodBanquetJoinInfo : list) {
            Map<Integer, DbParameter> para = new HashMap<>();
            int i = 1;
            goodBanquetJoinInfo.setBeginWriteTime();
            para.put(i++, new DbParameter(Types.BIGINT, goodBanquetJoinInfo.getUserId()));
            para.put(i++, new DbParameter(Types.BIGINT, goodBanquetJoinInfo.getTime()));
            para.put(i++, new DbParameter(Types.BIGINT, goodBanquetJoinInfo.getBanquetId()));
            para.put(i++, new DbParameter(Types.INTEGER, goodBanquetJoinInfo.getActivityId()));
            mapList.add(para);
            goodBanquetJoinInfo.setNoneOption();
        }

        return getDb().executeBatchQuery(updateGoodBanquetJoinInfoSql, mapList) > -1;
    }

    public static String insertGoodBanquetJoinInfoSql = "insert into `t_c_activity_good_banquet_join`(banquetId,activityId,userId,time) values (?,?,?,?)";
    @Override
    protected boolean addData(GoodBanquetJoinInfoData goodBanquetJoinInfo) {
        goodBanquetJoinInfo.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, goodBanquetJoinInfo.getBanquetId()));
        para.put(i++, new DbParameter(Types.INTEGER, goodBanquetJoinInfo.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, goodBanquetJoinInfo.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, goodBanquetJoinInfo.getTime()));
        if (getDb().executeNoneQuery(insertGoodBanquetJoinInfoSql, para) > 0) {
            return true;
        }
        return false;

    }

    /**
     * 批量更新据点信息
     * @param list
     * @return
     */
    public boolean batchAdd(List<GoodBanquetJoinInfoData> list){
        getDb().setConnName(DbNameStrategy.getCross2Db());
        List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
        for (GoodBanquetJoinInfoData goodBanquetJoinInfo : list) {
            Map<Integer, DbParameter> para = new HashMap<>();
            int i = 1;
            goodBanquetJoinInfo.setBeginWriteTime();
            para.put(i++, new DbParameter(Types.BIGINT, goodBanquetJoinInfo.getBanquetId()));
            para.put(i++, new DbParameter(Types.INTEGER, goodBanquetJoinInfo.getActivityId()));
            para.put(i++, new DbParameter(Types.BIGINT, goodBanquetJoinInfo.getUserId()));
            para.put(i++, new DbParameter(Types.BIGINT, goodBanquetJoinInfo.getTime()));
            mapList.add(para);
            goodBanquetJoinInfo.setNoneOption();
        }

        return getDb().executeBatchQuery(insertGoodBanquetJoinInfoSql, mapList) > -1;
    }


    @Override
    protected GoodBanquetJoinInfoData parseData(ResultSet rs) throws SQLException {
        GoodBanquetJoinInfoData goodBanquetJoinInfo = new GoodBanquetJoinInfoData();
        goodBanquetJoinInfo.setBanquetId(rs.getLong("banquetId"));
        goodBanquetJoinInfo.setActivityId(rs.getInt("activityId"));
        goodBanquetJoinInfo.setUserId(rs.getLong("userId"));
        goodBanquetJoinInfo.setTime(rs.getLong("time"));

        return goodBanquetJoinInfo;
    }
}
